package gwt.immunology.server.converter;

import static gwt.immunology.shared.GenericDtoKeys.ID;
import static gwt.immunology.shared.GenericDtoKeys.FIRST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.MIDDLE_NAME;
import static gwt.immunology.shared.GenericDtoKeys.LAST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.LOGIN;
import static gwt.immunology.shared.GenericDtoKeys.PASSWORD;
import static gwt.immunology.shared.GenericDtoKeys.POST;
import static gwt.immunology.shared.GenericDtoKeys.ROLES;
import gwt.immunology.shared.GenericDto;
import gwt.immunology.shared.ResultSetDto;

import java.util.List;

import models.Role;
import models.User;

public class UserDtoConverter {

    public static GenericDto convertUserToGenericDto(User user) {
	GenericDto result = new GenericDto();
	if (user != null) {
	    result.put(ID, user.userId);
	    result.put(FIRST_NAME, user.firstName);
	    result.put(MIDDLE_NAME, user.middleName);
	    result.put(LAST_NAME, user.lastName);
	    result.put(LOGIN, user.login);
	    result.put(PASSWORD, user.password);
	    result.put(POST, user.post);
	    result.put(ROLES, user.roles);
	}
	return result;
    }

    public static User convertGenericDtoToUser(GenericDto userDto) {
	User result = null;
	if (userDto != null && !userDto.isEmpty()) {
	    result = new User();
	    result.userId = userDto.getLong(ID);
	    result.firstName = userDto.getString(FIRST_NAME);
	    result.middleName = userDto.getString(MIDDLE_NAME);
	    result.lastName = userDto.getString(LAST_NAME);
	    result.login = userDto.getString(LOGIN);
	    result.password = userDto.getString(PASSWORD);
	    result.roles = (List<Role>) userDto.getList(ROLES);
	}
	return result;
    }

    public static ResultSetDto convertUsersToResultSetDto(List<User> users) {
	ResultSetDto result = new ResultSetDto();
	if (users != null && !users.isEmpty()) {
	    for (User user : users) {
		GenericDto userDto = convertUserToGenericDto(user);
		result.add(userDto);
	    }
	}
	return result;
    }

}
